Imports System
Imports System.Collections
Imports ASPNET.StarterKit.DataAccessLayer
Imports System.Collections.Generic

Namespace ASPNET.StarterKit.BusinessLogicLayer

	Public Class Project

		Private _ActualDuration As Decimal
		Private _CreatorUserName As String
		Private _CompletionDate As DateTime
		Private _DateCreated As DateTime
		Private _Description As String
		Private _EstimateDuration As Decimal
		Private _Id As Integer
		Private _ManagerUserName As String
        Private _Name As String
        Private _Disabled As Boolean

		Public Sub New(ByVal creatorUsername As String, ByVal managerUserName As String, ByVal name As String)

			Me.New(DefaultValues.GetDurationMinValue(), creatorUsername, DefaultValues.GetDateTimeMinValue(), DefaultValues.GetDateTimeMinValue(), String.Empty, DefaultValues.GetProjectDurationMinValue(), DefaultValues.GetProjectIdMinValue(), managerUserName, name)

		End Sub

		Public Sub New(ByVal creatorUsername As String, ByVal description As String, ByVal id As Integer, ByVal managerUserName As String, ByVal name As String)

			Me.New(DefaultValues.GetDurationMinValue(), creatorUsername, DefaultValues.GetDateTimeMinValue(), DefaultValues.GetDateTimeMinValue(), String.Empty, DefaultValues.GetProjectDurationMinValue(), DefaultValues.GetProjectIdMinValue(), managerUserName, name)

		End Sub

		Public Sub New(ByVal actualDuration As Decimal, ByVal creatorUserName As String, ByVal completionDate As DateTime, ByVal dateCreated As DateTime, ByVal description As String, ByVal estimateDuration As Decimal, ByVal id As Integer, ByVal managerUserName As String, ByVal name As String)

			' Validate Mandatory Fields

			If String.IsNullOrEmpty(creatorUserName) Then
				Throw New NullReferenceException("creatorUsername")
			End If

			If String.IsNullOrEmpty(managerUserName) Then
				Throw New NullReferenceException("managerUserName")
			End If

			If String.IsNullOrEmpty(name) Then
				Throw New NullReferenceException("name")
			End If

			Me._ActualDuration = actualDuration
			Me._CreatorUserName = creatorUserName
			Me._CompletionDate = completionDate
			Me._DateCreated = dateCreated
			Me._Description = description
			Me._EstimateDuration = estimateDuration
			Me._Id = id
			Me._ManagerUserName = managerUserName
			Me._Name = name

		End Sub

		Public ReadOnly Property ActualDuration() As Decimal

			Get
				Return Me._ActualDuration
			End Get

		End Property

		Public ReadOnly Property CreatorUserName() As String

			Get
				If String.IsNullOrEmpty(Me._CreatorUserName) Then
					Return String.Empty
				Else
					Return Me._CreatorUserName
				End If
			End Get

		End Property

		Public Property CompletionDate() As DateTime

			Get
				Return Me._CompletionDate
			End Get

			Set(ByVal value As DateTime)
				Me._CompletionDate = value
			End Set

		End Property

		Public ReadOnly Property DateCreated() As DateTime

			Get
				Return Me._DateCreated
			End Get

		End Property

		Public Property Description() As String

			Get
				If String.IsNullOrEmpty(Me._Description) Then
					Return String.Empty
				Else
					Return Me._Description
				End If
			End Get

			Set(ByVal value As String)
				Me._Description = value
			End Set

		End Property

		Public Property EstimateDuration() As Decimal

			Get
				Return Me._EstimateDuration
			End Get

			Set(ByVal value As Decimal)
				Me._EstimateDuration = value
			End Set

		End Property

		Public ReadOnly Property Id() As Integer

			Get
				Return Me._Id
			End Get

        End Property

		Public Property ManagerUserName() As String

			Get
				If String.IsNullOrEmpty(Me._ManagerUserName) Then
					Return String.Empty
				Else
					Return Me._ManagerUserName
				End If
			End Get

			Set(ByVal value As String)
				Me._ManagerUserName = value
			End Set

		End Property

		Public Property Name() As String

			Get
				If String.IsNullOrEmpty(Me._Name) Then
					Return String.Empty
				Else
					Return Me._Name
				End If
			End Get

			Set(ByVal value As String)
				Me._Name = value
			End Set

        End Property

        Public Property Disabled() As Boolean
            Get
                Return Me._Disabled
            End Get
            Set(ByVal value As Boolean)
                Me._Disabled = value
            End Set
        End Property

		' METHODS

		Public Function Delete() As Boolean

			If Me.Id > DefaultValues.GetProjectIdMinValue() Then
				Return DataAccessHelper.GetDataAccess().DeleteProject(Me.Id)
			Else
				Return False
			End If

		End Function

		Public Function Save() As Boolean

			Dim DALLayer As DataAccess = DataAccessHelper.GetDataAccess()
			If Me.Id <= DefaultValues.GetProjectIdMinValue() Then
				Dim TempId As Integer = DALLayer.CreateNewProject(Me)
				If TempId > DefaultValues.GetProjectIdMinValue() Then
					Me._Id = TempId
					Return True
				Else
					Return False
				End If
			Else
				Return DALLayer.UpdateProject(Me)
			End If

		End Function

		' STATIC METHODS

		Public Shared Function AddUserToProject(ByVal projectId As Integer, ByVal userName As String) As Boolean

			If projectId <= DefaultValues.GetProjectIdMinValue() Then
				Throw New ArgumentOutOfRangeException("projectId")
			End If

			If String.IsNullOrEmpty(userName) Then
				Throw New NullReferenceException("userName")
			End If

			Return DataAccessHelper.GetDataAccess().AddUserToProject(projectId, userName)

		End Function

		Public Shared Function DeleteProject(ByVal Id As Integer) As Boolean

			If Id <= DefaultValues.GetProjectIdMinValue() Then
				Throw New ArgumentOutOfRangeException("Id")
			End If

			Return DataAccessHelper.GetDataAccess().DeleteProject(Id)

		End Function

		Public Shared Function GetAllProjects() As List(Of Project)

			Return DataAccessHelper.GetDataAccess().GetAllProjects()

		End Function

		Public Shared Function GetAllProjects(ByVal sortParameter As String) As List(Of Project)

			Dim projectList As List(Of Project) = DataAccessHelper.GetDataAccess().GetAllProjects()

            If Not String.IsNullOrEmpty(sortParameter) Then
                projectList.Sort(New ProjectComparer(sortParameter))
            End If

			Return projectList

        End Function

        Public Shared Function GetAllDisabledProjects() As List(Of Project)

            Return DataAccessHelper.GetDataAccess().GetAllDisabledProjects()

        End Function

        Public Shared Function GetAllDisabledProjects(ByVal sortParameter As String) As List(Of Project)

            Dim projectList As List(Of Project) = DataAccessHelper.GetDataAccess().GetAllDisabledProjects()

            If Not String.IsNullOrEmpty(sortParameter) Then
                projectList.Sort(New ProjectComparer(sortParameter))
            End If

            Return projectList

        End Function

		Public Shared Function GetProjectById(ByVal Id As Integer) As Project

			If Id <= DefaultValues.GetProjectIdMinValue() Then
				Return Nothing
			End If

			Return DataAccessHelper.GetDataAccess().GetProjectById(Id)

		End Function

		Public Shared Function GetProjectByIds(ByVal ProjectIds As String) As List(Of Project)

			If String.IsNullOrEmpty(ProjectIds) Then
				Return New List(Of Project)()
			End If

			Dim separator As Char() = New Char() {","c}
            Dim substrings As String() = ProjectIds.Split(separator, StringSplitOptions.RemoveEmptyEntries)
			Dim list As New List(Of Project)()

			For Each str As String In substrings
				If Not String.IsNullOrEmpty(str) Then
					Dim id As Integer = CInt(str)
					list.Add(Project.GetProjectById(id))
				End If
			Next str

			Return list

		End Function


		Public Shared Function GetProjectsByManagerUserName(ByVal sortParameter As String, ByVal userName As String) As List(Of Project)

			If String.IsNullOrEmpty(userName) Then
				Return New List(Of Project)()
			End If

			Dim prjColl As List(Of Project) = DataAccessHelper.GetDataAccess().GetProjectsByManagerUserName(userName)
			If String.IsNullOrEmpty(sortParameter) Then
				prjColl.Sort(New ProjectComparer(sortParameter))
			End If

			Return prjColl

		End Function

		Public Shared Function GetProjectMembers(ByVal Id As Integer) As List(Of String)

			If Id <= DefaultValues.GetProjectIdMinValue() Then
				Return New List(Of String)()
			End If

			Return DataAccessHelper.GetDataAccess().GetProjectMembers(Id)

		End Function

		Public Shared Function GetProjectMembers(ByVal userNames As String) As List(Of String)

			If String.IsNullOrEmpty(userNames) Then
				Return New List(Of String)()
			End If

			Dim separator As Char() = New Char() {","c}
            Dim substrings As String() = userNames.Split(separator, StringSplitOptions.RemoveEmptyEntries)
			Dim list As New List(Of String)()

			For Each str As String In substrings
				If Not String.IsNullOrEmpty(str) Then
					Dim Id As Integer = CInt(str)
					Dim tempList As List(Of String) = Project.GetProjectMembers(Id)
					For Each userName As String In tempList
						list.Add(userName)
					Next userName
				End If
			Next str

			Return list

		End Function

		Public Shared Function GetProjectsByUserName(ByVal userName As String) As List(Of Project)

			If String.IsNullOrEmpty(userName) Then
				Return New List(Of Project)()
			End If

			Return DataAccessHelper.GetDataAccess().GetProjectsByUserName(userName)

		End Function

		Public Shared Function RemoveUserFromProject(ByVal projectId As Integer, ByVal userName As String) As Boolean

			If projectId <= DefaultValues.GetProjectIdMinValue() Then
				Throw New ArgumentOutOfRangeException("projectId")
			End If

			If String.IsNullOrEmpty(userName) Then
				Throw New NullReferenceException("userName")
			End If

			Return DataAccessHelper.GetDataAccess().RemoveUserFromProject(projectId, userName)

		End Function

	End Class

End Namespace
